package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.ActivityMapper;
import com.tarena.lbs.coupon.dao.repository.ActivityRepository;
import com.tarena.lbs.coupon.pojo.po.ActivityPO;
import com.tarena.lbs.coupon.pojo.query.ActivityPageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ActivityRepositoryImpl implements ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageInfo<ActivityPO> pageList(ActivityPageQuery query) {
        QueryWrapper<ActivityPO> queryWrapper = assembleActivityQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<ActivityPO> po = activityMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(po);
    }

    @Override
    public ActivityPO selectById(Integer id) {
        return activityMapper.selectById(id);
    }

    private QueryWrapper<ActivityPO> assembleActivityQuery(ActivityPageQuery query) {
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        if (query != null){
            if (query.getClass() != null){
                queryWrapper.like("activity_name",query.getClass());
            }
        }
        return queryWrapper;
    }
}
